//
//  ProjectController.m
//  ForAddSDK
//
//  Created by admin on 2018/1/9.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "ProjectController.h"
#import "DataHandler.h"

@interface ProjectController ()
@property (weak) IBOutlet NSTextField *gameChineseNameTextField;
@property (weak) IBOutlet NSTextField *bundleIDTextField;
@property (weak) IBOutlet NSTextField *gameVersionTextField;
@property (weak) IBOutlet NSPopUpButton *packChoice;
@property (weak) IBOutlet NSPopUpButton *ios_avcLocalSubPath;
@property (weak) IBOutlet NSTextField *localCachePathTextField;

@property (weak) IBOutlet NSTextField *packageSvnPathTextField;
@property (weak) IBOutlet NSTextField *packageCachePathTextField;

@property (unsafe_unretained) IBOutlet NSTextView *logTextView;

@end

@implementation ProjectController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
//    NSError *error;
//    NSURL *directoryURL = [[NSFileManager defaultManager] URLForDirectory:NSApplicationScriptsDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:&error];
//    [[NSWorkspace sharedWorkspace] openURL:directoryURL];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gameIDChange:) name:SelectedGameIDChangeNotification object:nil];
    if (_ios_avcLocalSubPath.numberOfItems == 3) {
        [_ios_avcLocalSubPath removeItemAtIndex:2];
    }
}

- (NSString *)getARCString
{
    //生成5-10位随机字符串
    int strLen = 5 + arc4random_uniform(10);
    char data[strLen];
    for (int x = 0; x < strLen; x++){
        data[x] = ('a' + (arc4random_uniform(26)));
    }
    NSString *arcString = [[NSString alloc] initWithBytes:data length:strLen encoding:NSUTF8StringEncoding];
    return arcString;
}

- (void)gameIDChange:(NSNotification *)notification {
    NSString *gameID = notification.object;
    if (![gameID isEqualToString:@"none"]) {
        NSDictionary *dic = [DataHandler getDefaultValuesWithGameID:gameID];
        if (dic) {
            _localCachePathTextField.stringValue = dic[COLUMNID_LOCAL_CACHE_PATH];
            _packageSvnPathTextField.stringValue = dic[COLUMNID_PACK_SVN_PATH];
            _packageCachePathTextField.stringValue = dic[COLUMNID_PACK_CACHE_PATH];
        }
    }
}

- (IBAction)bundleIDAndGameIDSet:(id)sender {
    DataHandler *handler = [DataHandler shareInstance];
    if (!handler.isPathOK || handler.workPlatform.length == 0 || handler.originPlatform.length == 0  || handler.projectSubPath.length == 0) {
        _logTextView.string = @"请回到路径相关页面设置有效路径和平台名";
    }
    else if (![DataHandler isExistPlatformProject:handler.workPlatform]) {
        _logTextView.string = [NSString stringWithFormat:@"%@工程不存在",handler.workPlatform];
    }
    else if (DEL_SPACE(_bundleIDTextField.stringValue).length || DEL_SPACE(_gameVersionTextField.stringValue).length || DEL_SPACE(_gameChineseNameTextField.stringValue).length ||
        _packChoice.indexOfSelectedItem != 0) {
        NSDictionary *userInfo = @{@"bundleID":DEL_SPACE(_bundleIDTextField.stringValue),@"gameVersion":DEL_SPACE(_gameVersionTextField.stringValue)};
        NSNotification *notification = [[NSNotification alloc] initWithName:BundleIDAndGameVersionChangeNotification object:nil userInfo:userInfo];
        [[NSNotificationCenter defaultCenter] postNotification:notification];
        
        NSString *xcodeProjPath = [NSString stringWithFormat:@"%@%@/proj.ios_appstore_%@/arpg.xcodeproj",handler.svnPath,handler.projectSubPath,handler.workPlatform];
        NSMutableArray *arguments = [NSMutableArray arrayWithObject:xcodeProjPath];
        [arguments addObject:handler.originPlatform];
        [arguments addObject:handler.workPlatform];
        [arguments addObject:DEL_SPACE(_bundleIDTextField.stringValue)];
        [arguments addObject:DEL_SPACE(_gameVersionTextField.stringValue)];
        [arguments addObject:DEL_SPACE(_gameChineseNameTextField.stringValue)];
        if (_packChoice.indexOfSelectedItem == 1) {
            [arguments addObject:@"big"];
        }
        else if (_packChoice.indexOfSelectedItem == 2) {
            [arguments addObject:@"small"];
        }
        else {
            [arguments addObject:@"none"];
        }
        handler.targetName = [self getARCString];
        [arguments addObject:handler.targetName];
        _logTextView.string = [DataHandler runScript:@"changeProject" type:@"rb" launchPath:@"/usr/bin/ruby" arguments:arguments];
    }
    else {
        _logTextView.string = @"请输入bundleID、游戏版本号、游戏中文名、设置大小包中至少一项";
    }
}

- (IBAction)localScriptPathSet:(id)sender {
    DataHandler *handler = [DataHandler shareInstance];
    if (!handler.isPathOK || handler.workPlatform.length == 0 || handler.projectSubPath.length == 0) {
        _logTextView.string = @"请回到路径相关页面设置有效路径和平台名";
    }
    else if (![DataHandler isExistPlatformProject:handler.workPlatform]) {
        _logTextView.string = [NSString stringWithFormat:@"%@工程不存在",handler.workPlatform];
    }
    else if (DEL_SPACE(_localCachePathTextField.stringValue).length || DEL_SPACE(_ios_avcLocalSubPath.selectedItem.title).length) {
        NSString *xcodeProjPath = [NSString stringWithFormat:@"%@%@/proj.ios_appstore_%@/arpg.xcodeproj",handler.svnPath,handler.projectSubPath,handler.workPlatform];
        NSMutableArray *arguments = [NSMutableArray arrayWithObject:xcodeProjPath];
        [arguments addObject:[NSString stringWithFormat:@"%@%@",handler.svnPath,DEL_SPACE(_ios_avcLocalSubPath.selectedItem.title)]];
        [arguments addObject:DEL_SPACE(_localCachePathTextField.stringValue)];
        _logTextView.string = [DataHandler runScript:@"changeRunScriptPath" type:@"rb" launchPath:@"/usr/bin/ruby" arguments:arguments];
    }
    else {
        _logTextView.string = @"请输入参数";
    }
}
- (IBAction)packageScriptPathSet:(id)sender {
    DataHandler *handler = [DataHandler shareInstance];
    if (!handler.isPathOK || handler.workPlatform.length == 0 || handler.projectSubPath.length == 0) {
        _logTextView.string = @"请回到路径相关页面设置有效路径和平台名";
    }
    else if (![DataHandler isExistPlatformProject:handler.workPlatform]) {
        _logTextView.string = [NSString stringWithFormat:@"%@工程不存在",handler.workPlatform];
    }
    else if (DEL_SPACE(_packageCachePathTextField.stringValue).length || (DEL_SPACE(_packageSvnPathTextField.stringValue).length && DEL_SPACE(_ios_avcLocalSubPath.selectedItem.title).length)) {
        NSString *xcodeProjPath = [NSString stringWithFormat:@"%@%@/proj.ios_appstore_%@/arpg.xcodeproj",handler.svnPath,handler.projectSubPath,handler.workPlatform];
        NSMutableArray *arguments = [NSMutableArray arrayWithObject:xcodeProjPath];
        NSString *ios_avc = @"";
        if (DEL_SPACE(_packageSvnPathTextField.stringValue).length && DEL_SPACE(_ios_avcLocalSubPath.selectedItem.title).length) {
            ios_avc = [NSString stringWithFormat:@"%@%@",DEL_SPACE(_packageSvnPathTextField.stringValue),DEL_SPACE(_ios_avcLocalSubPath.selectedItem.title)];
        }
        [arguments addObject:ios_avc];
        [arguments addObject:DEL_SPACE(_packageCachePathTextField.stringValue)];
        _logTextView.string = [DataHandler runScript:@"changeRunScriptPath" type:@"rb" launchPath:@"/usr/bin/ruby" arguments:arguments];
    }
    else {
        _logTextView.string = @"请输入参数";
    }
}

@end
